home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module10- qtzoo / completed source / buffereddrawer.java < prev    next >
Encoding:
Java Source  |  2000-09-28  |  5.4 KB  |  139 lines

  1. import java.awt.Container;
  2. import java.awt.Image;
  3. import java.awt.Graphics;
  4. import java.awt.Dimension;
  5.  
  6. /**
  7.  * A component which does all its drawing in an offscreen image
  8.  * the size of the component.
  9.  *
  10.  * @author Levi Brown
  11.  * @author Apple Worldwide Developer Technical Support
  12.  * @author Apple Computer Inc.
  13.  * @version 1.0 11/4/1998
  14.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  15.  *    
  16.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  17.  *                ("Apple") in consideration of your agreement to the following terms, and your
  18.  *                use, installation, modification or redistribution of this Apple software
  19.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  20.  *                please do not use, install, modify or redistribute this Apple software.
  21.  *
  22.  *                In consideration of your agreement to abide by the following terms, and subject
  23.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  24.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  25.  *                reproduce, modify and redistribute the Apple Software, with or without
  26.  *                modifications, in source and/or binary forms; provided that if you redistribute
  27.  *                the Apple Software in its entirety and without modifications, you must retain
  28.  *                this notice and the following text and disclaimers in all such redistributions of
  29.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  30.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  31.  *                Apple Software without specific prior written permission from Apple.  Except as
  32.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  33.  *                are granted by Apple herein, including but not limited to any patent rights that
  34.  *                may be infringed by your derivative works or by other works in which the Apple
  35.  *                Software may be incorporated.
  36.  *
  37.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  38.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  39.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  41.  *                COMBINATION WITH YOUR PRODUCTS.
  42.  *
  43.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  44.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  45.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  47.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  48.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  49.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50.  * 
  51.  */
  52. public class BufferedDrawer extends Container
  53. {
  54.     /**
  55.      * Creates a new BufferedDrawer.
  56.      */
  57.     public BufferedDrawer()
  58.     {
  59.         //Initialize our data members
  60.         workingGraphics     = null;
  61.         workingImage        = null;
  62.     }
  63.  
  64.     /**
  65.      * Handles drawing the desired content into the offscreen buffer.
  66.      * Override this to do your own drawing (be sure to call super.draw()).
  67.      * @see #paint
  68.      */
  69.     protected void draw()
  70.     {
  71.         //Make sure the offscreen image is ready to be drawn into.
  72.         ensureValidWorkingImage();
  73.  
  74.         //Do drawing here (use workingGraphics)
  75.         //Don't forget to erase the buffer if you don't draw over the entire area
  76.         //or else you will end up with tracers.
  77.         //To erase the entire area, uncomment out the following code:
  78.     }
  79.  
  80.     /**
  81.      * Makes sure the offscreen image exists.
  82.      */
  83.     protected void ensureValidWorkingImage()
  84.     {
  85.         //If the size changed and we need to create a new pane image, then create one
  86.         Dimension size = getSize();
  87.         if ((size != null && size.width > 0 && size.height > 0) && (workingImage == null || size.width != workingImage.getWidth(this) || size.height != workingImage.getHeight(this)))
  88.         {
  89.             if (workingImage != null)
  90.             {
  91.                 workingImage.flush();
  92.                 workingImage = null;
  93.             }
  94.             workingImage = createImage(size.width, size.height);
  95.             if (workingGraphics != null)
  96.             {
  97.                 workingGraphics.dispose();
  98.                 workingGraphics = null;
  99.             }
  100.             workingGraphics = workingImage == null ? null : workingImage.getGraphics();
  101.         }
  102.     }
  103.  
  104.     /** 
  105.      * Updates the component. This method is called in
  106.      * response to a call to repaint. You can assume that
  107.      * the background is not cleared.
  108.      * Overriden here to prevent unwanted background erasing.
  109.      * @param g the specified Graphics window
  110.      * @see #paint
  111.      * @see java.awt.Component#repaint
  112.      */
  113.     public void update(Graphics g)
  114.     {
  115.         paint(g);
  116.     }
  117.  
  118.     /** 
  119.      * Paints the component.  This method is called when the contents
  120.      * of the component should be painted in response to the component
  121.      * first being shown or damage needing repair.  The clip rectangle
  122.      * in the Graphics parameter will be set to the area which needs
  123.      * to be painted.
  124.      * @param g the specified Graphics object
  125.      * @see #update
  126.      */
  127.     public void paint(Graphics g)
  128.     {
  129.         draw();
  130.         g.drawImage(workingImage, 0, 0, this);
  131.         super.paint(g);
  132.     }
  133.  
  134.     //The offscreen image buffer to render the progress bar into.
  135.     protected Image workingImage;
  136.     //The Graphics object associated with the offscreen image buffer.
  137.     protected Graphics workingGraphics;
  138. }
  139.